﻿using System;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Renren.Models;
using Renren.Services;
using Renren.ViewModels;

namespace Renren.Pages
{
    public partial class NewsPage : PhoneApplicationPage
    {
        private NewsViewModel newsView;
        private bool messageBoxFocused = false;

        public NewsPage()
        {
            newsView = new NewsViewModel();
            InitializeComponent();
            this.MessageBox.Send += (sender, arg) =>
                {
                    FeedService.AddComment(newsView.Model, arg.Message);
                    var comment = new CommentModel();
                    comment.Name = App.LoginUser.Name;
                    comment.HeadUrl = App.LoginUser.TinyUrl;
                    comment.UpdateTime = DateTime.Now;
                    comment.Text = arg.Message;
                    newsView.Model.CommentList.Add(comment);
                    this.CommentsListBox.Focus();
                };

            this.MessageBox.GotFocus += (sender, arg) =>
                {
                    this.messageBoxFocused = true;
                    //this.CommentBox.Text = "";
                    for (int i = 0; i < this.CommentsListBox.Items.Count; i++)
                    {
                        var item = this.CommentsListBox.ItemContainerGenerator.ContainerFromIndex(i);
                        if (item != null)
                        {
                            (item as ListBoxItem).Opacity = 0.6;
                        }
                    }
                };

            this.MessageBox.LostFocus += (sender, arg) =>
                {
                    this.messageBoxFocused = false;
                    for (int i = 0; i < this.CommentsListBox.Items.Count; i++)
                    {
                        var item = this.CommentsListBox.ItemContainerGenerator.ContainerFromIndex(i);
                        if (item != null)
                        {
                            (item as ListBoxItem).Opacity = 1;
                        }
                    }
                };

            this.MessageBox.Tap += (s, e) =>
                {
                    if (!messageBoxFocused)
                    {
                        this.MessageBox.Text = "";
                    }
                };

            this.NewsPanel.Tap += (sender, arg) =>
                {
                    if (!messageBoxFocused)
                    {
                        var name = newsView.Model.Name;
                        this.MessageBox.Focus();
                        this.MessageBox.Text = "回复" + name + "：";
                        this.MessageBox.MessageTextBox.Select(this.MessageBox.Text.Length, 0);
                    }
                };
        }
                                    
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            //newsView.Update(PhoneApplicationService.Current.State["news"] as NewsModel);
            newsView.Model = PhoneApplicationService.Current.State["NewsPage"] as NewsModel;
            FeedService.GetComment(newsView.Model);
            DataContext = newsView;
        }

        private void CommentsListItem_Tap(object sender, GestureEventArgs e)
        {
            if (!messageBoxFocused)
            {
                var name = (this.CommentsListBox.SelectedItem as CommentModel).Name;
                this.MessageBox.Focus();
                this.MessageBox.Text = "回复" + name + "：";
                this.MessageBox.MessageTextBox.Select(this.MessageBox.Text.Length, 0);
            }
        }
    }
}